// 引入axios组件
import axios from 'axios';
import qs from 'qs'; // 处理数据  qs.stringify(params)
import { checkRule } from '@/assets/js/public';
// 引入公共js文件
export default {
  data() {
    return {
      title: '', // 面包屑标题
      search: '',
      modal1: false, // 模态框显示开关，true显示   false不显示
      addbrand: false, // 模态框显示开关，true显示   false不显示
      param: '', // 点击编辑、删除的时候对应操作的那个id
      labelname: '', // 标签名称
      id: '', // 标签id
      putpage: '', // 存储页数
      columns7: [
        {
          // type: 'index',
          title: '排序',
          width: 100,
          align: 'center',
          key: 'sort_no',
          // sortable:true,
          render: (h, params) => {
            const self = this;
            return h('div', [
              h('Input', {
                props: {
                  placeholder: '请输入',
                  value: params.row.sort_no,
                  // 'v-model':value
                },
                on: {
                  'on-blur': (event) => {
                    // 这是一个失去焦点事件
                    const sort = event.target.value;
                    this.getSort(sort, params.row.id);
                  },
                },
              }),
            ]);
          },
        },
        {
          title: '标签名称',
          key: 'labelname',
          render: (h, params) => h('div', [h('strong', params.row.labelname)]),
        },
        {
          title: '操作时间',
          key: 'addtime',
          render: (h, params) => h('div', [h('strong', params.row.addtime)]),
        },
        {
          title: '操作选项',
          key: 'action',
          width: 150,
          align: 'center',
          render: (h, params) =>
            h('div', [
              h(
                'Button',
                {
                  props: {
                    type: 'info',
                    size: 'small',
                  },
                  style: {
                    marginRight: '5px',
                  },
                  on: {
                    click: () => {
                      this.show(params.row.id);
                      // this.show(params);
                    },
                  },
                },
                '编辑',
              ),
              h(
                'Button',
                {
                  props: {
                    type: 'error',
                    size: 'small',
                  },
                  style: {
                    marginRight: '5px',
                  },
                  on: {
                    click: () => {
                      this.remove(params);
                    },
                  },
                },
                '删除',
              ),
            ]),
        },
      ],
    };
  },
  created() {
    const self = this;
    this.$Spin.show();
    setTimeout(() => {
      this.$Spin.hide();
    }, 300);
    const spage = this.$route.query.page;
    const asearch = this.$route.query.search;
    axios({
      method: 'post',
      url: '/admin/label/labelList',
      data: { page: spage, search: asearch },
    })
      .then((response) => {
        checkRule(response, self); // 检查是否有权限
        self.putpage = spage;
        self.$store.commit('getLabelParam', response.data);
      })
      .catch((response) => {
        console.log(response);
      });
  },
  // 用来获取VUEX中的文件
  computed: {
    data() {
      console.log(this.$store.state.label.labellistshow);
      return this.$store.state.label.labellistshow;
    },
    page() {
      console.log(this.$store.state.label.labellist.total);
      return this.$store.state.label.labellist.total;
    },
  },
  methods: {
    // 更新排序方法(完成)
    getSort(value, artid) {
      const self = this;
      for (const el of self.data) {
        console.log(el.id);
        if (el.id == artid) {
          el.sort_no = value;
        }
      }
      // 更新排序请求
      axios({
        method: 'post',
        url: '/admin/label/labelSort',
        data: {
          param: this.data,
        },
      })
        .then((response) => {
          checkRule(response, self); // 检查是否有权限
          self.$store.commit('getLabelParamAgain', self.putpage, self.search);
          self.$Message.success('操作成功');
        })
        .catch((response) => {
          // console.log('请求失败');
        });
    },
    // 添加品牌模态框
    addArticle() {
      this.param = '';
      this.labelname = '';
      this.cate_id = '';
      this.addbrand = true;
    },
    // 执行添加修改品牌
    ok_addbrand() {
      const self = this;
      axios({
        method: 'post',
        url: '/admin/label/labelUpdate',
        data: { labelname: this.labelname, id: this.param },
      })
        .then((response) => {
          checkRule(response, self); // 检查是否有权限
          if (response.data.code == 1) {
            self.$Message.success(response.data.msg);
            self.$store.commit('getLabelParamAgain', self.putpage, self.search);
            self.data6.splice(param.index, 1); // 如果把那个1改成0，就是不删除
          } else {
            self.$Message.error(response.data.msg);
          }
        })
        .catch((response) => {
          // self.$Message.error('请求失败');
        }); //* /
    },
    // 点击取消之后走的方法
    cancel_addbrand() {
      this.addbrand = false;
    },
    // 分页的钩子函数，当跳页的时候重新请求（将页码发给后台）
    getpage(page) {
      this.putpage = page;
      const self = this;
      // 分页
      axios({
        method: 'post',
        url: '/admin/label/labelList',
        data: {
          page,
          search: self.search,
        },
      })
        .then((response) => {
          self.$store.commit('getLabelParam', response.data);
        })
        .catch((response) => {
          console.log('请求失败');
        });
      this.$router.push({
        path: '/label', // 这是路由名称
        query: { page, search: self.search },
      });
    },
    /**
     * 编辑当前品牌
     * localStorage html5新定义的属性，存在兼容问题  使用方法参考下边网址
     * https://www.cnblogs.com/st-leslie/p/5617130.html
     * https://segmentfault.com/q/1010000010175869?sort=created
     */
    show(index) {
      const self = this;
      axios({
        method: 'post',
        url: '/admin/label/labelEdit',
        data: { labelid: index },
      })
        .then((response) => {
          checkRule(response, self); // 检查是否有权限
          self.param = response.data.labelinfo.id;
          self.labelname = response.data.labelinfo.labelname;
        })
        .catch((response) => {
          // self.$Message.error('请求失败');
        });
      this.addbrand = true;
    },
    /**
     * 删除当前标签
     * 同页面刷新不建议使用路由，再此请求数据
     * const定义的变量不可修改
     */
    remove(brandid) {
      this.modal1 = true;
      this.param = brandid;
    },
    ok() {
      const self = this; // 将this对象附到一个不可更改的变量中~
      axios({
        method: 'post',
        url: '/admin/label/labelDel',
        data: { id: self.param.row.id },
      })
        .then((response) => {
          checkRule(response, self); // 检查是否有权限
          if (response.data.code == 1) {
            const numm = response.data.count;
            const mo = numm % 10; // 判断剩余总数摸10是否是0
            const pagepage = Math.ceil(numm / 10);
            if (
              self.param.index == 0 &&
              self.putpage != 1 &&
              mo == 0 &&
              self.putpage != pagepage
            ) {
              self.putpage -= 1;
            }
            self.$store.commit('getLabelParamAgain', self.putpage, self.search);
            self.$Message.success(response.data.msg);
          } else {
            self.$Message.error(response.data.msg);
          }
        })
        .catch((response) => {
          // self.$Message.error('请求失败');
        });
    },
    cancel() {
      this.modal1 = false;
    },
  },
};
